<!DOCTYPE html>
<%@ page language="java" import="com.compuvest.web.*,java.util.*,java.text.*" pageEncoding="UTF-8" %>

<%@ taglib uri="/WEB-INF/tld/head.tld" prefix="head" %>

<jsp:useBean id="sb" class="SQLBean" scope="application"/>
<jsp:useBean id="advertShow" class="AdvertShow" scope="application"/>
<jsp:useBean id="mail" class="Mail" scope="application"/>
<jsp:useBean id="basket" class="Basket" scope="session"/>
<%!
    public static final String[] messagesCF = { 
/* 0 */	"<p>Your password is incorrect</p>",
/* 1 */	"<p>This email is not registered in our database. Please use 'New customer' section.</p>",
/* 2 */	"<p>Email or password cannot be empty</p>",
/* 3 */	"<p>You are a registered customer. Please use 'Returning customer' section above</p>",
/* 4 */	"<p><font color='green'>Your password has been sent to the email entered</font></p>",
/* 5 */	"If you are a returning customer, please log in to access your account.",
/* 6 */	"Choose a password for the ability to access your account in the future.",
/* 7 */	"Please enter your email address registered with your Compuvest account. We will email you password shortly."
    };
%>

<%
    String custURL = application.getInitParameter("https") + "Payment.jsp";
    String formURL = application.getInitParameter("https") + "CustomerForm.jsp";

    // logout
    if(request.getParameter("log") != null) {
        basket.setCustomer(null);
        basket.clean();

        response.sendRedirect(application.getInitParameter("http"));
        return;
    }

    if(basket.getCustomer() != null) {
        response.sendRedirect(custURL);
        return;
    }

    int forms = 0;
    String errorMess = "";
    String email = advertShow.getString(request.getParameter("Email"), "");
    String password = advertShow.getString(request.getParameter("Password"), "");
    String mode = request.getParameter("mode");
    if(mode != null) {
	if("old".equals(mode)) {
            forms = 1;
            if(!"".equals(email) && !"".equals(password)) {
		Customer cust = sb.getCustomer(email, password);
		if(cust.getCustomerID() > 0) { // present
                    basket.setCustomer(cust);
                    response.sendRedirect(custURL);
                    return;
		}
		else if(cust.getCustomerID() == 0) { // name present - passwd not correct
                    errorMess = messagesCF[0];
		}
		else if(cust.getCustomerID() == -1) { // new customer
                    errorMess = messagesCF[1];
		}
            }
            else errorMess = messagesCF[2];
	}
	else if("new".equals(mode)) {
            forms = 2;
            if(!"".equals(email) && !"".equals(password)) {
		int result = sb.checkCustomer(email);
		if(result == 0) { // make new
                    Customer cust = new Customer();
                    cust.setEmail(email);
                    cust.setPassword(password);
                    cust.setCustomerID(0);
                    cust.getBilling().setCountry("US");
                    cust.getShipping().setCountry("US");
                    cust.setType("C");

                    basket.setCustomer(cust);
                    response.sendRedirect(custURL);
                    return;
		}
		else if(result == -1) { // customer present
                    errorMess = messagesCF[3];
		}
            }
            else errorMess = messagesCF[2];
	}
	else if("fgot".equals(mode)) {
            forms = 3;
            if(!"".equals(email)) {
		Customer cust = sb.getPassword(email);
		if(cust.getCustomerID() != 0) {
                    errorMess = messagesCF[4];
                    mail.sendPassword(cust); // send password
		}
		else {
                    errorMess = messagesCF[1];
		}
            }
            else errorMess = messagesCF[2];
	}
    }
%>
<html>
<head>
<title><%= application.getInitParameter("CompanyName") %></title>
<%@include file="./incl/scripts.txt" %>
</head>
<body>

<div id='cvbody'>

 <head:show />

 <div id='cvcont'>
  <table id='cvmain'>
  <colgroup><col width='80px'/><col width='*'/><col width='80px'/></colgroup>
  <tbody>
   <tr>
    <td id='cvleft' valign="top">


    </td>
    <td id='cvcenter' align="center" valign="top">

<br/>
<form id="returncustomer" action="<%= formURL %>" method='POST' onSubmit='return isReady(this)' enctype="application/x-www-form-urlencoded">
<input type='hidden' name='mode' value='old'/>
<table id='cfrm_tb'>
 <colgroup><col span='2' width='50%'/></colgroup>
 <tr><td class='bsm' colspan='2'><span>Returning Customer</span></td></tr>
 <tr height='90'>
  <td><div><%= (forms == 1) ? errorMess : messagesCF[5] %></div></td>
  <td class='brd'>
   <p>e-mail : <input maxlength='64' type='text' name='Email'/></p>
   <p>Password : <input maxlength='15' type='password' name='Password'/></p>
  </td>
 </tr>
 <tr>
  <td class='bsm' colspan='2' align='right'>
   <input type='submit' value='Go to my account'/>
  </td>
 </tr>
</table>
</form>
<br/>

<%
    // no basket - no new customer
    if(basket.notEmpty()) {
%>
<form id="newcustomer" action="<%= formURL %>" method='POST' onSubmit='return isReady(this)' enctype="application/x-www-form-urlencoded">
<input type='hidden' name='mode' value='new'/>
<table id='cfrm_tb'>
 <colgroup><col span='2' width='50%'/></colgroup>
 <tr><td class='bsm' colspan='2'><span>New Customer</span></td></tr>
 <tr height='90'>
  <td><div><%= (forms == 2) ? errorMess : messagesCF[6] %></div></td>
  <td class='brd'>
   <p>e-mail : <input maxlength='64' type='text' name='Email'/></p>
   <p>Password : <input maxlength='15' type='password' name='Password'/></p>
   <p>Confirm Password : <input maxlength='15' type='password' name='vPassword'/></p>
  </td>
 </tr>
 <tr><td class='bsm' colspan='2' align='right'><input type='submit' value='Create Account'/></td></tr>
</table>
</form>
<br/>
<%
    }
%>

<form id="forgotcustomer" action="<%= formURL %>" onSubmit='return isReady(this)' method='POST' enctype="application/x-www-form-urlencoded">
<input type='hidden' name='mode' value='fgot'/>
<table id='cfrm_tb'>
 <colgroup><col span='2' width='50%'/></colgroup>
 <tr><td class='bsm' colspan='2'><span>Forgot your password ?</span></td></tr>
 <tr height='90'>
  <td><div><%= (forms == 3) ? errorMess : messagesCF[7] %></div></td>
  <td class='brd'>
   <p>e-mail : <input maxlength='64' type='text' name='Email'/></p>
  </td>
 </tr>
 <tr><td class='bsm' colspan='2' align='right'><input type='submit' value='S u b m i t'/></td></tr>
</table>
</form>

    </td>
    <td id='cvright' valign="top">

    </td>
   </tr>
  </tbody>
  </table>
 </div>

 <div id='cvfoot'></div>
</div>

</body>
</html>
